


//
//  ZSearchResultVC.m
//  ZQLearn
//
//  Created by 朵 on 2019/5/10.
//  Copyright © 2019 macfeel. All rights reserved.
#import "ZBookMsgDetailVC.h"
#import "ReadModel.h"
#import "ZSearchResultVC.h"
#import "SearchBar.h"
#import "OrderCell.h"//借用订单cell
@interface ZSearchResultVC ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)SearchBar *searchBar;

@end

#define cellIdentifier @"order"

@implementation ZSearchResultVC

#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(60, 7, ScreenWidth-120, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    bar.souImg.hidden = YES;
    bar.barBtn.hidden = YES;
    bar.textFd.hidden = false;
    bar.textFd.delegate = self;
    self.searchBar = bar;
    //    self.navigationItem.titleView = bar;
    [self.navigationController.navigationBar addSubview:bar];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.searchBar.hidden = false;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchBar.hidden = YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 105.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        WeakStament(ws);
//        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
//            [ws refreshData];
//        }];
        
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //背景蓝
    //    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
    ////这个仅仅是读书的搜索页面 融媒体和首页分别有自己的搜索页面
    [self.navigationItem setItemWithTitle:@"搜索结果" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 45, 45) title:@"搜索" Image:@"" selectedIMG:@"" tartget:self action:@selector(sousuoAction)];
    
    [self addSearchBar];
    self.dataArray = [NSMutableArray array];
    ///添加的顺序要变一下，可能是数据回来后再生成
    [self.view addSubview:self.tableView];
}

- (void)loadData{
    NSDictionary *dic;
    NSString *url;
    if ([self.flag isEqualToString:@"read"]) {
        dic = @{@"name":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"30",};
        url = kReadBookSearchUrl;
    }else if ([self.flag isEqualToString:@"med"]){
        ////
        dic = @{@"title":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"30",};
        url = kNewsListUrl;
    }
    
    if (self.currentPage==1) {
        
    }
    [self.dataArray removeAllObjects];
    
    [ZNetworkService getRequestWithBaseUrl:url setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
             [self.searchBar.textFd resignFirstResponder];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

///
- (void)sousuoAction{
    ///搜索接口请求
    [self loadData];
}


#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.flag isEqualToString:@"read"]) {
    ReadModel *model = self.dataArray[indexPath.row];
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.numLab.hidden = YES;
    cell.jifenLab.textColor = kDarkGrayColor;
    cell.nameLab.text = model.bookInfo[@"name"];
    cell.jifenLab.text = model.bookInfo[@"content"];
    cell.topHeight.constant = 20.f;
    cell.bottomHeight.constant = 50.f;
//    cell.numLab.text =[NSString stringWithFormat:@"X%@",subModel.commodityNumber];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.bookInfo[@"photo"]]] placeholderImage:nil];
    return cell;
    }
        return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadModel *hmodel = self.dataArray[indexPath.row];
    ZBookMsgDetailVC *det = [ZBookMsgDetailVC new];
    MidModel *model = [MidModel new];
    model.name = hmodel.name;
    model.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,hmodel.photo];
    det.MModel = model;
    [self.navigationController pushViewController:det animated:YES];
}


@end
